package com.student.service;

import com.student.bean.Student;
import com.student.dao.StudentDAOImpl;
import com.student.dao.util.JDBCUtils;
import org.junit.jupiter.api.Test;

import java.sql.*;
import java.util.List;
import java.util.Properties;

/**
 * @ClassName studentService
 * @Description 对学生表的增、删、查、改 等操作
 * @Author wk
 * @Date 2022/3/3 16:14
 * @Version 1.0
 */
public class StudentService {

    private StudentDAOImpl studentDAO = new StudentDAOImpl();

    /**
     * @author wk
     * @Description 添加学生信息
     * @Date 16:27 2022/3/3
     * @Param
     * @Return
     */
    public int addStudent(Student student) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return  studentDAO.insert(connection, student);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 删除学生信息
     * @Date 16:44 2022/3/3
     * @Param
     * @Return
     */
    public int deleteStudent(String studentId) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.delete(connection, studentId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 根据学号，修改学生部分信息
     * @Date 16:48 2022/3/3
     * @Param
     * @Return
     */

    public int update(String studentId, String key, Object value) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.update(connection, studentId, key, value);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 根据修改全部信息
     * @Date 16:50 2022/3/3
     * @Param
     * @Return
     */

    public int updateAll(String oldStudentId, Student student) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.updateAll(connection, oldStudentId, student);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 根据学号查询学生信息
     * @Date 16:56 2022/3/3
     * @Param
     * @Return
     */

    public Object search(String studentId) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.getStudentByStudentId(connection, studentId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 查找全部学生信息
     * @Date 16:58 2022/3/3
     * @Param
     * @Return
     */

    public List<Student> searchAll() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.getStudentAll(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 获取学生总数
     * @Date 16:59 2022/3/3
     * @Param
     * @Return
     */

    public Long getCount() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.getCount(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }


    /**
     * @author wk
     * @Description 清空所有学生的信息
     * @Date 20:09 2022/3/4
     * @Param
     * @Return
     */

    public int clearAll() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.clearAll(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * 通过学生id，检查学生是否存在
     *
     * @param studentId 学生id
     * @return boolean
     */

    public boolean checkStudentIsExistById(String studentId) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.checkStudentIsExistById(connection, studentId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * 通过手机号，检查学生是否存在
     *
     * @param phone 电话
     * @return boolean
     */
    public boolean checkStudentIsExistByPhone(String phone) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.checkStudentIsExistByPhone(connection, phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * 通过身份证号，检查学生是否有重复
     *
     * @param card 身份证号
     * @return boolean
     */
    public boolean checkStudentIsExistByCard(String card) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.checkStudentIsExistByCard(connection, card);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * @author wk
     * @Description 检查用户输入的字段是否存在
     * @Date 22:04 2022/3/3
     * @Param
     * @Return
     */

    public boolean checkExist(String value) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "select student_id,student_name,student_age,student_sex,student_phone,student_card,student_location," +
                    "student_english,student_math,student_java from student";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 获取结果集
            ResultSet resultSet = preparedStatement.executeQuery();
            // 获取结果集的元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            // 获取每一行的列数
            int columnCount = metaData.getColumnCount();
            String columnLabel = "";
            for (int i = 0; i < columnCount; i++) {
                // 获取每一行的每一列的别名
                columnLabel = metaData.getColumnLabel(i + 1);
                if (value.equals(columnLabel)) {
                    return true;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * @author wk
     * @Description 如果名字为两个字，则在名字中间添加空格，使其长度为3
     * @Date 23:42 2022/3/3
     * @Param
     * @Return
     */

    public String changeName(String name) {
        if (name.length() == 2) {
            name = name.substring(0, 1) + " " + name.substring(name.length() - 1);
        }
        return name;
    }
}
